package com.libbase.activity

import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.PointF
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.DecelerateInterpolator
import com.base.animation.item.BitmapDisplayItem
import com.base.animation.model.AnimPathObject
import com.base.animation.model.PathObject
import com.libbase.R
import com.uitls.utils.DisplayUtils
import kotlinx.android.synthetic.main.activity_anim_canvas.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi

/**
 * @author:zhouzechao
 * @date: 2020/11/17
 * description：
 */
@ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
class TestAnimCanvasActivity : AppCompatActivity() {

    private var i: Int = 0

    companion object {

        @JvmStatic
        fun startActivity(context: Context) {
            val intent = Intent(context, TestAnimCanvasActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_anim_canvas)

        anim_1?.setOnClickListener {
            startSingleAnim()
        }

        anim_2?.setOnClickListener {
            startMoreAnim()
        }
    }

    override fun onResume() {
        super.onResume()
        anim_surface?.resume()
    }

    override fun onPause() {
        super.onPause()
        anim_surface?.pause()
    }

    private fun startSingleAnim() {
        val size = 80
        val bitmap = BitmapFactory.decodeResource(this@TestAnimCanvasActivity.resources,
            R.mipmap.anim)
        val bitmapDisplayItem =
            BitmapDisplayItem.of(bitmap).apply {
                displayItemId = R.mipmap.anim.toString()
                attachView(anim_surface.width, anim_surface.height)
            }
        val bitmapDisplayItem2 =
            BitmapDisplayItem.of(BitmapFactory.decodeResource(this@TestAnimCanvasActivity.resources,
                R.mipmap.gift_banner_combo_bg)).apply {
                displayItemId = R.mipmap.gift_banner_combo_bg.toString()
                attachView(anim_surface.width, anim_surface.height)
            }
        val start = PathObject(bitmapDisplayItem.displayItemId, point = PointF(0f,
            0f), interpolator = DecelerateInterpolator(), scaleX = 0.5f, scaleY = 0.5f)
        val next = PathObject(bitmapDisplayItem2.displayItemId, point = PointF(
            DisplayUtils.getScreenWidth(this@TestAnimCanvasActivity).toFloat() / 2,
            DisplayUtils.getScreenHeight(this@TestAnimCanvasActivity).toFloat() / 2),
            interpolator = AccelerateDecelerateInterpolator(), scaleX = 1.5f, scaleY = 1.5f)
        val next1 = PathObject(bitmapDisplayItem2.displayItemId, point = PointF(
            DisplayUtils.getScreenWidth(this@TestAnimCanvasActivity).toFloat() / 2,
            9f), scaleX = 0.5f, scaleY = 0.5f)
        val next2 = PathObject(bitmapDisplayItem2.displayItemId, point = PointF(
            DisplayUtils.getScreenWidth(this@TestAnimCanvasActivity).toFloat() / 2,
            9f), scaleX = 1.0f, scaleY = 1.0f)
        val next3 = PathObject(bitmapDisplayItem2.displayItemId, point = PointF(
            DisplayUtils.getScreenWidth(this@TestAnimCanvasActivity).toFloat() / 2,
            9f), scaleX = 0.5f, scaleY = 0.5f)
        anim_surface?.addAnimDisplay(
            AnimPathObject.Inner.with(mutableListOf(bitmapDisplayItem, bitmapDisplayItem2))
                .beginAnimPath(start)
                .doAnimPath(1000, next)
                .beginNextAnimPath(next)
                .doAnimPath(1000, next1)
                .beginNextAnimPath(next1)
                .doAnimPath(200, next2)
                .beginNextAnimPath(next2)
                .doAnimPath(200, next3)
                .build())
    }

    private fun startMoreAnim() {
        val bitmapDisplayItem =
            BitmapDisplayItem.of(BitmapFactory.decodeResource(this@TestAnimCanvasActivity.resources,
                R.mipmap.anim)).apply {
                displayItemId = R.mipmap.anim.toString()
                attachView(
                    anim_surface.width, anim_surface.height)
            }
        val start = PathObject(bitmapDisplayItem.displayItemId, point = PointF(0f,
            0f), interpolator = DecelerateInterpolator())
        val next = PathObject(bitmapDisplayItem.displayItemId, point = PointF(
            DisplayUtils.getScreenWidth(this@TestAnimCanvasActivity).toFloat() / 2,
            DisplayUtils.getScreenHeight(this@TestAnimCanvasActivity).toFloat() / 2),
            interpolator = AccelerateDecelerateInterpolator())
        val next1 = PathObject(bitmapDisplayItem.displayItemId, point = PointF(
            DisplayUtils.getScreenWidth(this@TestAnimCanvasActivity).toFloat() / 2,
            0f), rotation = 360f)
        val next2 = PathObject(bitmapDisplayItem.displayItemId, point = PointF(
            0f,
            DisplayUtils.getScreenHeight(this@TestAnimCanvasActivity).toFloat() / 2), alpha = 0)
        val next3 = PathObject(bitmapDisplayItem.displayItemId, point = PointF(
            DisplayUtils.getScreenWidth(this@TestAnimCanvasActivity).toFloat() / 2,
            DisplayUtils.getScreenHeight(this@TestAnimCanvasActivity).toFloat()), scaleX = 0f,
            scaleY = 0f)
        val next4 = PathObject(bitmapDisplayItem.displayItemId, point = PointF(
            DisplayUtils.getScreenWidth(this@TestAnimCanvasActivity).toFloat(),
            DisplayUtils.getScreenHeight(this@TestAnimCanvasActivity).toFloat() / 2))
        anim_surface?.addAnimDisplay(
            AnimPathObject.Inner.with(bitmapDisplayItem).beginAnimPath(start)
                .doAnimPath(1500, next)
                .beginNextAnimPath(next)
                .doAnimPaths(1500, mutableListOf(next1, next2, next3, next4))
                .build())
    }

    override fun onDestroy() {
        super.onDestroy()
        anim_surface?.endAnimation()
    }
}